package com.example.wanandroid.fragment;

import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.example.wanandroid.R;
import static com.example.wanandroid.R.id.home_content_layout;

public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView mBottomNavigationView;

    private SelectFragment selectFragment;
    private SystemFragment movieFragment;
    private NavigationFragment musicFragment;
    private MeFragment meFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mBottomNavigationView = (BottomNavigationView) view.findViewById(R.id.mBottomNavigationView);
        //设置监听
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
        //去掉渲染
        mBottomNavigationView.setItemIconTintList(null);

        showSelectFragment();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_select:
                showSelectFragment();
                break;
            case R.id.menu_moive:
                showMovieFragment();
                break;
            case R.id.menu_music:
                showMusicFragment();
                break;
            case R.id.menu_me:
                showMeFragment();
                break;
        }
        return true;
    }

    private void showSelectFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (selectFragment == null) {
            selectFragment = new SelectFragment();
            ft.add(home_content_layout, selectFragment);
        }
        hideAllFragment(ft);
        ft.show(selectFragment);
        ft.commit();
    }

    private void showMovieFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (movieFragment == null) {
            movieFragment = new SystemFragment();
            ft.add(home_content_layout, movieFragment);
        }
        hideAllFragment(ft);
        ft.show(movieFragment);
        ft.commit();
    }

    private void showMusicFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (musicFragment == null) {
            musicFragment = new NavigationFragment();
            ft.add(home_content_layout, musicFragment);
        }
        hideAllFragment(ft);
        ft.show(musicFragment);
        ft.commit();
    }

    private void showMeFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (meFragment == null) {
            meFragment = new MeFragment();
            ft.add(home_content_layout, meFragment);
        }
        hideAllFragment(ft);
        ft.show(meFragment);
        ft.commit();
    }

    //隐藏所有的Fragment
    private void hideAllFragment(FragmentTransaction fb) {
        if (selectFragment != null) {
            fb.hide(selectFragment);
        }
        if (movieFragment != null) {
            fb.hide(movieFragment);
        }
        if (musicFragment != null) {
            fb.hide(musicFragment);
        }
        if (meFragment != null) {
            fb.hide(meFragment);
        }
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
        if (selectFragment == null && childFragment instanceof SelectFragment) {
            selectFragment = (SelectFragment) childFragment;
        } else if (movieFragment == null && childFragment instanceof SystemFragment) {
            movieFragment = (SystemFragment) childFragment;
        } else if (musicFragment == null && childFragment instanceof NavigationFragment) {
            musicFragment = (NavigationFragment) childFragment;
        } else if (meFragment == null && childFragment instanceof MeFragment) {
            meFragment = (MeFragment) childFragment;
        }
    }
}
